/*
 * draw_detected_corners.cpp
 *
 *  Created on: Jan 21, 2013
 *      Author: filipe
 */

#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <string>
#include <cstdio>

using namespace std;

typedef struct
{
	string corners_data_filename;
	int num_corners_x;
	int num_corners_y;

}DrawDetectedCornersData;

DrawDetectedCornersData program_data;


int
parse_arguments(int argc, char *argv[])
{
	if (argc < 4)
		return 0;
	else
	{
		program_data.corners_data_filename = argv[1];
		program_data.num_corners_x = atoi(argv[2]);
		program_data.num_corners_y = atoi(argv[3]);

		return 1;
	}
}


void
show_usage_information_and_exit(int argc, char *argv[])
{
	printf("\nUse %s <corner-data-filename> <num corners y> <num corners x>\n", argv[0]);
	exit(-1);
}


int
main(int argc, char *argv[])
{
	if (!parse_arguments(argc, argv))
		show_usage_information_and_exit(argc, argv);

	FILE *file = fopen(program_data.corners_data_filename.c_str(), "r");

	char image_filename[1024], sx[64], sy[64];
	int num_points = program_data.num_corners_x * program_data.num_corners_y;
	double x, y;
	int ix, iy;

	while(!feof(file))
	{
		fscanf(file, "\n%s", image_filename);
		IplImage *img = cvLoadImage(image_filename, CV_LOAD_IMAGE_COLOR);

		for(int i = 0; i < num_points; i++)
		{
			fscanf(file, " %s %s ", sx, sy);

			x = atof(sx);
			y = atof(sy);

			ix = (int) x;
			iy = (int) y;

			cvCircle(img, cvPoint(ix, iy), 2, cvScalar(0, 0, 255, 0), 1, 1, 0);
		}

		cvShowImage("img", img);
		cvWaitKey(-1);

		cvReleaseImage(&img);
	}

	fclose(file);
	return 0;
}

